package cn.stronglink.asset.module.sys.permission.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.stronglink.asset.model.Permission;
import cn.stronglink.asset.module.sys.permission.service.PermissionService;
import cn.stronglink.asset.module.sys.permission.vo.PermissionVo;
import cn.stronglink.asset.module.sys.role.vo.RoleVo;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.exception.BusinessException;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;

@Controller
@RequestMapping(value = "sys/per")
public class PermissionController extends AbstractController {
	
	@Autowired
	private PermissionService permissionService;
	
	@RequestMapping(value = "qryPermission")
	public ResponseEntity<ModelMap> qryPermission(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {
		List<Permission> list = permissionService.qryPermission(params);
		return setSuccessModelMap(modelMap, list);
	}
	
	/**
	 * 获取所有的权限
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "qryAllPermission")
	public ResponseEntity<ModelMap> qryAllPermission(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		List<PermissionVo> list=permissionService.getAllPermission();
		return setSuccessModelMap(modelMap, list);
	}
	
	/**
	 * 获取角色所属的权限
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "qryRolePermission")
	public ResponseEntity<ModelMap> qryRolePermission(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		List<String> list=permissionService.getRolePermission(id);
		return setSuccessModelMap(modelMap, list);
	}
	
	/**
	 * 设置角色所属的权限
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@OperateLog(module = "角色管理",desc="设置角色权限", type = OpType.UPDATE)
	@RequestMapping(value = "setRolePermission")
	public ResponseEntity<ModelMap> setRolePermission(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody RoleVo vo) {
		permissionService.setRolePermission(vo);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	/**
	 * 添加角色接口
	 * @param role
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@OperateLog(module = "角色管理",desc="添加角色信息", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> createPermission(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Permission permission) {
		permissionService.insertPermission(permission);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除角色
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@OperateLog(module = "角色管理",desc="删除角色信息", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		Integer counts=permissionService.getPermissionUsers(id);
		if(counts>0){
			throw new BusinessException("角色包含用户，不能删除");
		}
		permissionService.delPermission(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	/**
	 * 更新角色信息
	 * @param role
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@OperateLog(module = "角色管理",desc="编辑角色信息", type = OpType.UPDATE)
	@RequestMapping(value = "update")
	public ResponseEntity<ModelMap> update(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Permission permission) {
		permissionService.updatePermissionById(permission);
		return setSuccessModelMap(modelMap, null);
	}
	
	
	
}
